package com.kuang.bx.service;

import com.kuang.bx.dao.BlogDAO;
import com.kuang.bx.dao.CategoryDAO;
import com.kuang.bx.pojo.Blog;
import com.kuang.bx.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Service
public class BlogService {
    @Autowired
    private BlogDAO blogDAO;

    @Autowired
    CategoryService categoryService;

    public List<Blog> List() {
        Sort sort = new Sort(Sort.Direction.DESC,"id");
        return blogDAO.findAll(sort);
    }

    public List<Blog> listByCategory(int cid){
        Category category = categoryService.get(cid);
        return  blogDAO.findAllByCategory(category);
    }

    public void addOrUpdate(Blog blog){
        blogDAO.save(blog);
    }

    public void deleteAndSortFindBlog(int id){
        blogDAO.deleteById(id);
    }

    public List<Blog> Search(String keywords){
        return blogDAO.findAllByTitleOrAuthor('%'+keywords+'%','%'+'%'+keywords+'%');
    }

}
